package com.edu.exam.entity.quality.monitoring;

import com.edu.exam.enums.PatrolFileAccessLevel;
import com.edu.exam.enums.PatrolFileBackupStatus;
import com.edu.exam.enums.PatrolFileCategory;
import com.edu.exam.enums.PatrolFileProcessingStatus;
import com.edu.exam.enums.PatrolFileStatus;
import com.edu.exam.enums.PatrolFileVirusScanStatus;
import jakarta.persistence.*;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import lombok.AllArgsConstructor;
import lombok.experimental.SuperBuilder;

import java.time.LocalDateTime;

// External enum imports
import com.edu.exam.enums.quality.monitoring.FileStatus;
import com.edu.exam.enums.quality.monitoring.FileCategory;
import com.edu.exam.enums.quality.monitoring.AccessLevel;
import com.edu.exam.enums.quality.monitoring.VirusScanStatus;
import com.edu.exam.enums.quality.monitoring.BackupStatus;
import com.edu.exam.enums.quality.monitoring.ProcessingStatus;

/**
 * 巡考文件管理实体类
 *
 * 负责管理巡考过程中的文件上传、存储和访问
 * 根据E-05-巡考管理模块设计实现
 *
 * @author System
 * @version 1.0.0
 */
@Entity
@Data
@NoArgsConstructor
@AllArgsConstructor
@SuperBuilder
@EqualsAndHashCode(callSuper = false)
public class PatrolFile {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "file_id")
    private Long id;

    @Column(name = "record_id")
    private Long recordId;

    @Column(name = "violation_id")
    private Long violationId;

    @Column(name = "file_name", nullable = false, length = 255)
    private String fileName;

    @Column(name = "original_name", nullable = false, length = 255)
    private String originalName;

    @Column(name = "file_path", nullable = false, length = 500)
    private String filePath;

    @Column(name = "file_url", nullable = false, length = 500)
    private String fileUrl;

    @Column(name = "file_size", nullable = false)
    private Long fileSize; // 文件大小（字节）

    @Column(name = "file_type", nullable = false, length = 50)
    private String fileType;

    @Column(name = "mime_type", length = 100)
    private String mimeType;

    @Column(name = "upload_user_id", nullable = false)
    private Long uploadUserId;

    @Column(name = "upload_user_name", length = 100)
    private String uploadUserName;

    @Column(name = "upload_time", nullable = false)
    private LocalDateTime uploadTime;

    @Enumerated(EnumType.STRING)
    @Column(name = "status", nullable = false, length = 20)
    private PatrolFileStatus status = PatrolFileStatus.UPLOADED;

    @Column(name = "description", length = 500)
    private String description; // 文件描述

    @Column(name = "category", length = 50)
    @Enumerated(EnumType.STRING)
    private PatrolFileCategory category; // 文件分类

    @Column(name = "access_level", length = 20)
    @Enumerated(EnumType.STRING)
    private PatrolFileAccessLevel accessLevel = PatrolFileAccessLevel.NORMAL; // 访问级别

    @Column(name = "download_count")
    private Integer downloadCount = 0; // 下载次数

    @Column(name = "last_access_time")
    private LocalDateTime lastAccessTime; // 最后访问时间

    @Column(name = "last_access_user_id")
    private Long lastAccessUserId; // 最后访问用户ID

    @Column(name = "last_access_user_name", length = 100)
    private String lastAccessUserName; // 最后访问用户姓名

    @Column(name = "virus_scan_status", length = 20)
    @Enumerated(EnumType.STRING)
    private PatrolFileVirusScanStatus virusScanStatus = PatrolFileVirusScanStatus.PENDING; // 病毒扫描状态

    @Column(name = "virus_scan_time")
    private LocalDateTime virusScanTime; // 病毒扫描时间

    @Column(name = "virus_scan_result", length = 200)
    private String virusScanResult; // 病毒扫描结果

    @Column(name = "checksum", length = 64)
    private String checksum; // 文件校验和

    @Column(name = "backup_status", length = 20)
    @Enumerated(EnumType.STRING)
    private PatrolFileBackupStatus backupStatus = PatrolFileBackupStatus.PENDING; // 备份状态

    @Column(name = "backup_time")
    private LocalDateTime backupTime; // 备份时间

    @Column(name = "backup_path", length = 500)
    private String backupPath; // 备份路径

    @Column(name = "retention_period")
    private Integer retentionPeriod; // 保留期限（天）

    @Column(name = "expiry_date")
    private LocalDateTime expiryDate; // 过期时间

    @Column(name = "auto_delete_enabled")
    private Boolean autoDeleteEnabled = false; // 是否启用自动删除

    @Column(name = "archived")
    private Boolean archived = false; // 是否已归档

    @Column(name = "archive_time")
    private LocalDateTime archiveTime; // 归档时间

    @Column(name = "archive_path", length = 500)
    private String archivePath; // 归档路径

    @Column(name = "compressed")
    private Boolean compressed = false; // 是否已压缩

    @Column(name = "compression_ratio")
    private Double compressionRatio; // 压缩比率

    @Column(name = "encrypted")
    private Boolean encrypted = false; // 是否已加密

    @Column(name = "encryption_algorithm", length = 50)
    private String encryptionAlgorithm; // 加密算法

    @Column(name = "watermarked")
    private Boolean watermarked = false; // 是否添加水印

    @Column(name = "watermark_type", length = 50)
    private String watermarkType; // 水印类型

    @Column(name = "tags", length = 500)
    private String tags; // 标签（逗号分隔）

    @Column(name = "metadata", columnDefinition = "TEXT")
    private String metadata; // 元数据（JSON格式）

    @Column(name = "thumbnail_url", length = 500)
    private String thumbnailUrl; // 缩略图URL

    @Column(name = "preview_url", length = 500)
    private String previewUrl; // 预览URL

    @Column(name = "dimensions", length = 100)
    private String dimensions; // 图片尺寸（宽x高）

    @Column(name = "duration") // 视频/音频时长（秒）
    private Integer duration;

    @Column(name = "resolution", length = 50)
    private String resolution; // 分辨率

    @Column(name = "quality_score")
    private Double qualityScore; // 质量评分

    @Column(name = "usage_count")
    private Integer usageCount = 0; // 使用次数

    @Column(name = "shared_with", columnDefinition = "TEXT")
    private String sharedWith; // 分享对象（JSON格式）

    @Column(name = "sharing_enabled")
    private Boolean sharingEnabled = false; // 是否启用分享

    @Column(name = "sharing_expiry")
    private LocalDateTime sharingExpiry; // 分享过期时间

    @Column(name = "download_limit")
    private Integer downloadLimit; // 下载限制次数

    @Column(name = "ip_restriction", length = 200)
    private String ipRestriction; // IP访问限制

    @Column(name = "geo_restriction", length = 200)
    private String geoRestriction; // 地理位置限制

    @Column(name = "access_log", columnDefinition = "TEXT")
    private String accessLog; // 访问日志（JSON格式）

    @Column(name = "processing_status", length = 20)
    @Enumerated(EnumType.STRING)
    private PatrolFileProcessingStatus processingStatus = PatrolFileProcessingStatus.COMPLETED; // 处理状态

    @Column(name = "processing_error", columnDefinition = "TEXT")
    private String processingError; // 处理错误信息

    @Column(name = "retry_count")
    private Integer retryCount = 0; // 重试次数

    @Column(name = "max_retries")
    private Integer maxRetries = 3; // 最大重试次数

    @Column(name = "created_time", nullable = false, updatable = false)
    private LocalDateTime createdTime;

    @Column(name = "updated_time", nullable = false)
    private LocalDateTime updatedTime;

    @Column(name = "created_by", length = 100)
    private String createdBy;

    @Column(name = "updated_by", length = 100)
    private String updatedBy;

    @Column(name = "version_number")
    private Integer versionNumber = 1; // 版本号

    @Column(name = "parent_file_id")
    private Long parentFileId; // 父文件ID（用于版本管理）

    @Column(name = "is_latest_version")
    private Boolean isLatestVersion = true; // 是否为最新版本


}